define(function (require) {
    require('common/fileuploader');
    require('validate');
    var Base = require('group/common/main');
    var utils = require('common/utils/home');
    var Mask = require('common/mask');
    var mask = new Mask();
    var AgreementModal = require('group/common/agreement_modal');

    return Base.extend({
        initialize: function (params) {
            var _this = this;
            Base.prototype.initialize.apply(this, [params]);
            if (_this['page_' + _global.a]) {
                _this['page_' + _global.a]();
            }
        },
        page_login: function () {
            var _this = this;
            _this.$('#J_login_form').validate({
                submitHandler: function (form) {
                    var params = _this.$('form').serializeJson();
                    utils.ajaxPost(_.extend(params), function (resp) {
                        mask.success('登录成功', function () {
                            if (resp.url) {
                                window.location.href = resp.url;
                            }
                            else {
                                window.location.href = _.U({m: 'user', a: 'index'});
                            }
                        });
                    });
                }
            });
        },
        page_register: function () {
            var _this = this;
            /*if (_this.$('input[name="step"]').val() == 2) {
                new AgreementModal().show({a: 'ajax_agreement'});
            }*/

            $('.J_form').validate({
                submitHandler: function (form) {
                    var params = _this.$('form').serializeJson();
                    utils.ajaxPost(_.extend(params), function (resp) {
						if (resp.url) {
							window.location.href = resp.url;
						}
						else {
							window.location.href = _.U({m: 'user', a: 'index'});
						}
                    });
                }
            });
            $('.J_sms').click(function () {
                var $this = $(this);
                var tele = $('form input[name="tele"]').val();
				if(checkMobile(tele)==false){ 
					mask.error("手机号码有误，请重填"); 
					return false;
				} 
                if (tele) {
                    utils.ajaxPost({'tele': tele, a: 'get_register_code'}, function (resp) {
                        if (resp['status'] == 0) {
                            mask.error(resp.msg);
                            return false;
                        } else {
							countDown($this);
                        }
                    });
                }
                else {
                    mask.error('请输入您的手机号');
                    return false;
                }
				function checkMobile(str){
					var re = /^1\d{10}$/;
					return re.test(str) ? true : false;
				}
				var cutdowntime = 120;
				function countDown(o){
					if(cutdowntime==0){
						$(o).removeClass('disabled');    
						$(o).html('获取验证码');
						cutdowntime = 120;
						return;
					}else{
						$(o).addClass('disabled');
						$(o).html(cutdowntime+'秒后重发'); 
						cutdowntime--;
					}
					setTimeout(function(){countDown(o)},1000);
				}
            });
            $('.btn-box img').click(function(){
                var url = $(this).attr('src');
                $(this).attr('src', url);
            });
        },
        page_forget_password: function () {
            if (PAGE_STEP == 1) {
                $('.js-submit-btn').click(function () {
                    var tele = $('form input[name="tele"]').val();
                    var code = $('form input[name="code"]').val();
                    if (tele == '') {
                        tip('请输入您的手机号');
                        return false;
                    }
                    if (code == '') {
                        tip('请输入手机验证码');
                        return false;
                    }
                    utils.ajaxPost({
                        'tele': tele,
                        'code': code,
                        'forget_step': 2,
                        a: 'forget_password'
                    }, function (resp) {
                        if (resp['status'] == 0) {
                            tip(resp.msg);
                            return false;
                        }
                        else {
                            window.location.href = resp.data.ret_url;
                        }
                    });
                });
                $('.J_sms').click(function () {
                    var $this = $(this);
                    var tele = $('form input[name="tele"]').val();

                    if (tele) {
                        utils.ajaxPost({'tele': tele, a: 'get_forget_code'}, function (resp) {
                            if (resp['status'] == 0) {
                                tip(resp.msg);
                                return false;
                            }
                            else {
                                var seconds = 60;
                                $this.removeClass('J_sms');
                                setInterval(function () {
                                    $this.text(seconds + '秒后重发');
                                    if (seconds > 0) {
                                        seconds--;
                                    }
                                    else {
                                        $this.html('获取验证码');
                                        $this.addClass('J_sms');
                                        return false;
                                    }
                                }, 1000);
                            }
                        });
                    }
                    else {
                        tip('请输入您的手机号');
                        return false;
                    }
                });
            }
            else if (PAGE_STEP == 2) {
                $('.js-submit-btn').click(function () {
                    var $this = $(this);
                    var password = $('form input[name="password"]').val();
                    if (password.length < 6 || password.length > 16) {
                        tip('请输入6~16位数密码');
                        return false;
                    }
                    utils.ajaxPost({'password': password, 'forget_step': 3, a: 'forget_password'}, function (resp) {
                        if (resp['status'] == 0) {
                            tip(resp.msg);
                            return false;
                        }
                        else {
                            window.location.href = resp.data.ret_url;
                        }
                    });
                });
            }
        }
    });
});